package com.meiqiongproperty.yiguanjia.fragment.pay;

import android.content.Context;

import com.meiqiongproperty.yiguanjia.httpbean.life.LifeWXPay;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderRepayWXBean;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayWX;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.WaterWX;
import com.tencent.mm.sdk.constants.Build;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * author: Administrator
 * date: 2017/5/28.
 * package:com.meiqiongproperty.yiguanjia.fragment.pay
 */

public class WXPayYGJ {
    private static WXPayYGJ mWXPayYGJ;
    private IWXAPI mWXApi;
    private CommPayWX.ParameterBeanX.ParameterBean mPayParam;
    private LifeWXPay.ParameterBeanLifeX.ParameterBean mLifePayParam;
    private WaterWX.ParameterBeanX.ParameterBean mWaterPayParam;
    private ShopOrderRepayWXBean.ParameterBeanX.ParameterBean mShopOrderRepayParam;
    private WXPayYGJ.WXPayYGJResultCallBack mCallback;

    public static final int NO_OR_LOW_WX = 1;   //未安装微信或微信版本过低
    public static final int ERROR_PAY_PARAM = 2;  //支付参数错误
    public static final int ERROR_PAY = 3;  //支付失败

    public interface WXPayYGJResultCallBack {
        void onSuccess(); //支付成功

        void onError(int error_code);   //支付失败

        void onCancel();    //支付取消
    }

    public WXPayYGJ(Context context, String wx_appid) {
        mWXApi = WXAPIFactory.createWXAPI(context, null);
        mWXApi.registerApp(wx_appid);
    }

    public static void init(Context context, String wx_appid) {
        if (mWXPayYGJ == null) {
            mWXPayYGJ = new WXPayYGJ(context, wx_appid);
        }
    }

    public static WXPayYGJ getInstance() {
        return mWXPayYGJ;
    }

    public IWXAPI getWXApi() {
        return mWXApi;
    }

    /**
     * 发起微信支付
     */
    public void doPay(CommPayWX.ParameterBeanX.ParameterBean pay_param, WXPayYGJ.WXPayYGJResultCallBack callback) {
        mPayParam = pay_param;
        mCallback = callback;

        if (!check()) {
            if (mCallback != null) {
                mCallback.onError(NO_OR_LOW_WX);
            }
            return;
        }

        PayReq req = new PayReq();
        req.appId = mPayParam.getAppid();
        req.partnerId = mPayParam.getPartnerid();
        req.prepayId = mPayParam.getPrepayid();
        req.packageValue = mPayParam.getPackageX();
        req.nonceStr = mPayParam.getNoncestr();
        req.timeStamp = mPayParam.getTimestamp();
        req.sign = mPayParam.getSign();

        mWXApi.sendReq(req);
    }

    /**
     * 发起微信支付
     */
    public void doLifePay(LifeWXPay.ParameterBeanLifeX.ParameterBean pay_param, WXPayYGJ.WXPayYGJResultCallBack callback) {
        mLifePayParam = pay_param;
        mCallback = callback;

        if (!check()) {
            if (mCallback != null) {
                mCallback.onError(NO_OR_LOW_WX);
            }
            return;
        }

        PayReq req = new PayReq();
        req.appId = mLifePayParam.getAppid();
        req.partnerId = mLifePayParam.getPartnerid();
        req.prepayId = mLifePayParam.getPrepayid();
        req.packageValue = mLifePayParam.getPackageX();
        req.nonceStr = mLifePayParam.getNoncestr();
        req.timeStamp = mLifePayParam.getTimestamp();
        req.sign = mLifePayParam.getSign();

        mWXApi.sendReq(req);
    }


    /**
     * 发起微信支付 商场订单重新支付
     */
    public void doShopOrderRepay(ShopOrderRepayWXBean.ParameterBeanX.ParameterBean pay_param, WXPayYGJ.WXPayYGJResultCallBack callback) {
        mShopOrderRepayParam = pay_param;
        mCallback = callback;

        if (!check()) {
            if (mCallback != null) {
                mCallback.onError(NO_OR_LOW_WX);
            }
            return;
        }

        PayReq req = new PayReq();
        req.appId = mShopOrderRepayParam.getAppid();
        req.partnerId = mShopOrderRepayParam.getPartnerid();
        req.prepayId = mShopOrderRepayParam.getPrepayid();
        req.packageValue = mShopOrderRepayParam.getPackageX();
        req.nonceStr = mShopOrderRepayParam.getNoncestr();
        req.timeStamp = mShopOrderRepayParam.getTimestamp();
        req.sign = mShopOrderRepayParam.getSign();

        mWXApi.sendReq(req);
    }

    /**
     * 发起微信支付 商场订单重新支付
     */
    public void doWaterOrderRepay(WaterWX.ParameterBeanX.ParameterBean pay_param, WXPayYGJ.WXPayYGJResultCallBack callback) {
        mWaterPayParam = pay_param;
        mCallback = callback;

        if (!check()) {
            if (mCallback != null) {
                mCallback.onError(NO_OR_LOW_WX);
            }
            return;
        }

        PayReq req = new PayReq();
        req.appId = mWaterPayParam.getAppid();
        req.partnerId = mWaterPayParam.getPartnerid();
        req.prepayId = mWaterPayParam.getPrepayid();
        req.packageValue = mWaterPayParam.getWxpackage();
        req.nonceStr = mWaterPayParam.getNoncestr();
        req.timeStamp = String.valueOf(mWaterPayParam.getTimestamp());
        req.sign = mWaterPayParam.getSign();

        mWXApi.sendReq(req);
    }


    //支付回调响应
    public void onResp(int error_code) {
        if (mCallback == null) {
            return;
        }

        if (error_code == 0) {   //成功
            mCallback.onSuccess();
        } else if (error_code == -1) {   //错误
            mCallback.onError(ERROR_PAY);
        } else if (error_code == -2) {   //取消
            mCallback.onCancel();
        }

        mCallback = null;
    }

    //检测是否支持微信支付
    private boolean check() {
        return mWXApi.isWXAppInstalled() && mWXApi.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
    }
}
